<?php



// +----------------------------------------------------------------------

// | OneThink [ WE CAN DO IT JUST THINK IT ]

// +----------------------------------------------------------------------

// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.

// +----------------------------------------------------------------------

// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>

// +----------------------------------------------------------------------



namespace Home\Controller;



use Think\Controller;

use think\Image;

/**

 * 相册控制器

 */

class AlbumController extends Controller {



    /**

     * [showList description]展示图片列表

     * @return [type] [description]

     */

    public function showList() {

        $id = I('get.id');

        $this->assign('id', $id);

        //查找所有的图片

        $all = D('Album')->showAll($id);

		
        $this->assign('all', $all);

        //过滤已经删除完的图片

//        $data = D('Album')->delNull($all);

//        //把图片字符串转为数组

//        $res = D('Album')->selectImg($data);

//        $this->assign('res', $res);

        $uid = $_SESSION['onethink_home']['uid'];

        $this->assign('uid', $uid);

        $this ->assign('imgid',session('imgid'));

        $this->display();

    }



    /**

     * [addAlbum description]添加图片

     */

    public function addAlbum() {

        $post = I('post.');

        $data = D('Album')->addAlbum($post);

        if ($data) {

            $this->success('添加图片成功', U('showList', array('id' => $post['id'])));

        } else {

            $this->error('添加图片失败');

        }

    }



    /**

     * [ajaxDel description]ajax删除数据

     * @return [type] [description]

     */

    public function ajaxDel() {

        $post = I('post.');

        $data = D('Album')->ajaxDel($post);

        if ($data) {

            $this->success("删除成功！");

        } else {

            $this->error("删除失败！");

        }

    }



    public function addd() {

        $id = session('imgid');
        

        if(empty($id)){

            $this ->error('没有图片不可以上传');

        }else{

        $return = D('Album')->add_alub();

        $data = count(explode(",",$return));

        if($data<4){

            if ($return) {

                $img = $return . ',' . $id;

                if (D('Album')->alub_save(session('uid'), $img)) {

                    session('imgid',null);

                    $this->success('添加成功');

                } else {

                    session('imgid',null);

                    $this->error('添加失败');

                }

            } else {

                $arr['id'] = session('uid');

                $arr['imgs'] = $id;

                $return = D('Album')->addAlbum($arr);

                if ($return) {

                    session('imgid',null);

                    $this->success('添加成功');

                } else {

                    session('imgid',null);

                    $this->error('添加失败');

                }

            }

        }else{

            $this->error('一天只能发布4张照片');

        }

        }

    }

    public function imgs(){ 

        $aa = $_FILES;

        //$host = $_SERVER['HTTP_HOST']; //获取当前域名

        // $image = new \Think\Image();

        // $image->open($aa['img']['tmp_name']);

        // // // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg

        // $image->thumb(200, 200)->save('./Uploads/simg/'.$aa['img']['name']);

        // $aa['img']['tmp_name'] = $host.__ROOT__.'/Uploads/simg/'.$aa['img']['name'];

        // $uid = $_SESSION['onethink_home']['uid'];

        // $arr['rootPath'] = "./Uploads/Picture/";

        // dump($arr);

        // dump($aa);

        // dump('./Uploads/simg/'.$aa['img']['name']);

        // die;

        $aa['img']['is_thumb']=true;

        $file = D('Picture')->upload_thumb($aa, $arr);

        if(D('Album') ->addImgOnt($file['img']['id'])){

            //$this ->success('添加成功');
			echo '添加成功';
			die;

        }else{

            //$this ->error('上传的图片不能大于2M');
			
			echo '上传的图片不能大于2M';
			die;

        }

        

    }

}

